//
//  WriteCustomerViewController.m
//  Rail
//
//  Created by hychen on 2018/7/22.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "WriteCustomerViewController.h"
#import "TextFieldTableViewCell.h"
#import "ButtonTableViewCell.h"

@interface WriteCustomerViewController ()
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *phone;
@property (nonatomic, copy) NSString *company;
@end

@implementation WriteCustomerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"填写访客"];
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    [self.tableView yj_addTapDismissKeyboardGesture];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BMWeakSelf
    if (indexPath.row < 3) {
        TextFieldTableViewCell *cell = [TextFieldTableViewCell cellWithInitStyleTableView:tableView class:[TextFieldTableViewCell class]];
        cell.tf.keyboardType = UIKeyboardTypeDefault;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setBlockEndEditing:^(NSString *text) {
            switch (indexPath.row) {
                case 0:
                    weakSelf.name = text;
                    break;
                case 1:
                    weakSelf.phone = text;
                    break;
                case 2:
                    weakSelf.company = text;
                    break;
            }
            [weakSelf.tableView reloadData];
        }];
        switch (indexPath.row) {
            case 0:
                cell.title = @"访客姓名:";
                cell.content = self.name;
                cell.placeholder = @"请输入访客姓名";
                break;
            case 1:
                cell.title = @"手机号码:";
                cell.content = self.phone;
                cell.placeholder = @"请输入手机号码";
                break;
            case 2:
                cell.title = @"单位名称:";
                cell.content = self.company;
                cell.placeholder = @"请输入单位名称";
                break;
        }
        return cell;
    }
    else if (indexPath.row == 3) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.buttonTitle = @"保存";
        BMWeakSelf
        [cell setBlockClickEventButton:^{
            [weakSelf handleSubmit];
        }];
        return cell;
    }
    return [UITableViewCell new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 3) {
        return 45;
    }
    else if (indexPath.row == 3) {
        return 200;
    }
    return 0;
}

- (void)handleSubmit {
    [self.view endEditing:YES];
    if (![self.name objectIsNotEmpty]) {
        Show_Msg_Error(@"请填写访客姓名")
        return;
    }
    else if (![self.phone objectIsNotEmpty]) {
        Show_Msg_Error(@"请填写手机号码")
        return;
    }
    else if (![self.company objectIsNotEmpty]) {
        Show_Msg_Error(@"请填写单位名称")
        return;
    }
    else if (![CheckUtility checkForMobilePhoneNo:self.phone]) {
        Show_Msg_Error(@"填写正确手机号码")
        return;
    }
    DeviceUserInfoModel *model = [DeviceUserInfoModel new];
    model.username = self.name;
    model.mobile = self.phone;
    model.company = self.company;
    if (self.BlockSelectCustomer) {
        self.BlockSelectCustomer(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
